import request from '@/utils/request'
import type { ApiResponse } from '@/types/api'
import type { Customer } from '@/types/customer'

// 客户列表响应接口
export interface CustomerListResponse {
  list: Customer[]
  total: number
  page: number
  limit: number
}

// 客户统计信息接口
export interface CustomerStatistics {
  total: number
  active: number
  cooperation: number
  to_follow: number
  overdue: number
  conversion_rate: number
  member_customers?: number
  contract_customers?: number
  case_customers?: number
}

// 高级筛选参数
export interface CustomerSearchParams {
  page?: number
  limit?: number
  keyword?: string
  user_id?: number
  department_id?: number
  level?: number
  important_level?: number
  status?: number
  is_cooperation?: number
  industry?: string
  scale?: string
  source?: string
  followup_filter?: string
  create_time_start?: number
  create_time_end?: number
  cooperation_time_start?: number
  cooperation_time_end?: number
  tags?: string | string[]
  customer_status?: string
  business_type?: string
  type?: number
}

// 选项接口
export interface CustomerOptions {
  user_options: Array<{ value: number; label: string }>
  dept_options: Array<{ value: number; label: string }>
  industry_options: Array<{ value: string; label: string }>
  scale_options: Array<{ value: string; label: string }>
  source_options: Array<{ value: string; label: string }>
}

// 客户API
export const customerApi = {
  // 获取客户列表
  getCustomerList: (params: CustomerSearchParams) => {
    return request({
      url: '/api/customer/customer/list',
      method: 'get',
      params,
    })
  },

  // 搜索客户
  search: (params: { keyword: string; page?: number; limit?: number }) => {
    return request({
      url: '/api/customer/customer/search',
      method: 'get',
      params,
    })
  },

  // 查看客户详情
  getCustomerDetail: (params: { id: number; contract_workflow?: number }) => {
    // 确保contract_workflow参数被正确传递，默认为1以绕过权限检查
    const requestParams = {
      ...params,
      contract_workflow: params?.contract_workflow || 1,
    }
    return request({
      url: '/api/customer/customer/view',
      method: 'get',
      params: requestParams,
    })
  },

  // 新增客户
  addCustomer: (data: Partial<Customer>) => {
    return request({
      url: '/api/customer/customer/add',
      method: 'post',
      data,
    })
  },

  // 编辑客户
  updateCustomer: (data: Partial<Customer> & { id: number }) => {
    return request({
      url: '/api/customer/customer/edit',
      method: 'put',
      data,
    })
  },

  // 删除客户
  deleteCustomer: (data: { id: number }) => {
    return request({
      url: '/api/customer/customer/del',
      method: 'delete',
      data,
    })
  },

  // 废弃客户
  discardCustomer: (data: { id: number; reason: string }) => {
    return request({
      url: '/api/customer/customer/discard',
      method: 'post',
      data,
    })
  },

  // 创建当事人
  createPartyFromCustomer: (data: { customer_id: number }) => {
    return request({
      url: '/api/customer/customer/create_party_from_customer',
      method: 'post',
      data,
    })
  },

  // 获取状态选项
  getStatusOptions: () => {
    return request({
      url: '/api/customer/customer/status',
      method: 'get',
    })
  },

  // 获取基础数据（选项）
  getOptions: () => {
    return request({
      url: '/api/customer/customer/basic_data',
      method: 'get',
    })
  },

  // 获取客户统计信息
  getStatistics: () => {
    return request({
      url: '/api/customer/customer/statistics',
      method: 'get',
    })
  },

  // ============================================
  // 客户联系人管理 API
  // ============================================

  // 添加客户联系人
  addContact: (data: {
    customer_id: number
    name: string
    mobile?: string
    position?: string
    is_default?: number
    qq?: string
    wechat?: string
    email?: string
    department?: string
    birthday?: number
    address?: string
  }) => {
    return request({
      url: '/api/customer/customer/add_contact',
      method: 'post',
      data,
    })
  },

  // 更新客户联系人
  updateContact: (data: {
    id: number
    name?: string
    mobile?: string
    position?: string
    is_default?: number
    qq?: string
    wechat?: string
    email?: string
    department?: string
    birthday?: number
    address?: string
  }) => {
    return request({
      url: '/api/customer/customer/update_contact',
      method: 'put',
      data,
    })
  },

  // 删除客户联系人
  deleteContact: (data: { id: number }) => {
    return request({
      url: '/api/customer/customer/delete_contact',
      method: 'delete',
      data,
    })
  },

  // ============================================
  // 客户跟进记录管理 API
  // ============================================

  // 添加客户跟进记录
  addTrace: (data: {
    customer_id: number
    content: string
    types?: number
    stage?: number
    contact_id?: number
    follow_time?: number
    next_time?: number
  }) => {
    return request({
      url: '/api/customer/customer/add_trace',
      method: 'post',
      data,
    })
  },

  // 更新客户跟进记录
  updateTrace: (data: {
    id: number
    content?: string
    types?: number
    stage?: number
    contact_id?: number
    follow_time?: number
    next_time?: number
  }) => {
    return request({
      url: '/api/customer/customer/update_trace',
      method: 'put',
      data,
    })
  },

  // 删除客户跟进记录
  deleteTrace: (data: { id: number }) => {
    return request({
      url: '/api/customer/customer/delete_trace',
      method: 'delete',
      data,
    })
  },

  // ============================================
  // 客户机会线索管理 API
  // ============================================

  // 添加客户机会线索
  addChance: (data: {
    customer_id: number
    title: string
    stage?: number
    content?: string
    expected_sign_date?: number
    expected_order_amount?: number
    level?: number
    source?: string
  }) => {
    return request({
      url: '/api/customer/customer/add_chance',
      method: 'post',
      data,
    })
  },

  // 更新客户机会线索
  updateChance: (data: {
    id: number
    title?: string
    stage?: number
    content?: string
    expected_sign_date?: number
    expected_order_amount?: number
    level?: number
    source?: string
    status?: number
  }) => {
    return request({
      url: '/api/customer/customer/update_chance',
      method: 'put',
      data,
    })
  },

  // 删除客户机会线索
  deleteChance: (data: { id: number }) => {
    return request({
      url: '/api/customer/customer/delete_chance',
      method: 'delete',
      data,
    })
  },
}

export default customerApi
